import { defineStore } from 'pinia'
import { useAccountLoginApi, useMobileLoginApi, useLogoutApi, useThirdLoginApi } from '@/api/auth'
import cache from '@/utils/cache'

/* 本地数据模拟 */
const userInfoMock = {
	data: {
		id: 10000,
		username: 'admin',
		realName: 'admin',
		avatar: 'https://cdn.maku.net/images/avatar.png',
		gender: 0,
		email: 'babamu@126.com',
		mobile: '13612345678',
		orgId: null,
		status: 1,
		roleIdList: null,
		postIdList: [],
		postNameList: null,
		superAdmin: 1,
		orgName: null,
		createTime: '2024-01-05 14:31:11'
	}
}
const authorityListMock = {
	data: [
		'tenant:package:delete',
		'tenant:tenant:save',
		'tenant:package:update',
		'sys:role:page',
		'sys:user:export',
		'sys:sms:config',
		'flow:category:all',
		'tenant:package:save',
		'schedule:run',
		'sys:attachment:delete',
		'sys:operate:all',
		'sys:user:page',
		'sys:mail:config',
		'flow:instance:all',
		'sys:role:list',
		'tenant:package:page',
		'sys:post:save',
		'sys:dict:info',
		'sys:org:info',
		'sys:post:delete',
		'sys:role:save',
		'sys:third:config',
		'monitor:user:all',
		'tenant:tenant:delete',
		'online:interface',
		'sys:org:update',
		'monitor:cache:all',
		'sys:role:update',
		'sys:mail:log',
		'sys:log:login',
		'sys:org:list',
		'sys:post:page',
		'sys:user:save',
		'schedule:delete',
		'tenant:datasource:delete',
		'flow:model:all',
		'tenant:package:info',
		'monitor:server:all',
		'tenant:datasource:page',
		'online:report:all',
		'sys:menu:update',
		'sys:menu:delete',
		'schedule:info',
		'sys:dict:delete',
		'sys:menu:list',
		'sys:params:all',
		'sys:dict:page',
		'flow:process:all',
		'sys:user:import',
		'tenant:tenant:update',
		'sys:user:delete',
		'tenant:datasource:save',
		'sys:org:save',
		'sys:menu:info',
		'sys:user:update',
		'pay:order:all',
		'schedule:log',
		'sys:role:menu',
		'sys:post:info',
		'tenant:tenant:info',
		'sys:post:update',
		'schedule:update',
		'tenant:datasource:info',
		'sys:menu:save',
		'sys:sms:log',
		'schedule:page',
		'tenant:datasource:update',
		'sys:org:delete',
		'sys:role:info',
		'sys:attachment:page',
		'sys:user:info',
		'sys:attachment:save',
		'sys:role:delete',
		'schedule:save',
		'online:table:all',
		'pay:notify:all',
		'tenant:tenant:page',
		'sys:dict:update',
		'sys:dict:save'
	]
}
const accountLoginInfoMock = {
	code: 0,
	msg: 'success',
	data: {
		accessTokenExpire: '2024-11-27 22:35:16',
		refreshTokenExpire: '2024-12-11 20:35:16',
		access_token: 'cbe2db1506ed44d89fc2d7a2b058c471',
		refresh_token: 'd3f7e22c301f4081bf993a105e8fad85'
	}
}
export const useUserStore = defineStore('userStore', {
	state: () => ({
		// 用户信息
		user: {
			id: '',
			superAdmin: 0,
			username: '',
			realName: '',
			gender: 0,
			mobile: '',
			email: '',
			avatar: '',
			orgName: '',
			postNameList: [],
			createTime: ''
		},
		// 权限列表
		authorityList: [] as string[],
		// 访问token
		token: cache.getToken(),
		// 刷新token
		refreshToken: cache.getRefreshToken()
	}),
	actions: {
		setUser(val: any) {
			this.user = val
		},
		setToken(val: any) {
			this.token = val
			cache.setToken(val)
		},
		setRefreshToken(val: any) {
			this.refreshToken = val
			cache.setRefreshToken(val)
		},
		// 账号登录
		async accountLoginAction(loginForm: any) {
			// const { data } = await useAccountLoginApi(loginForm)
			const { data } = accountLoginInfoMock
			this.setToken(data.access_token)
			this.setRefreshToken(data.refresh_token)
		},
		// 手机号登录
		async mobileLoginAction(loginForm: any) {
			const { data } = await useMobileLoginApi(loginForm)
			this.setToken(data.access_token)
			this.setRefreshToken(data.refresh_token)
		},
		// 第三方登录
		async thirdLoginAction(loginForm: any) {
			const { data } = await useThirdLoginApi(loginForm)
			this.setToken(data.access_token)
			this.setRefreshToken(data.refresh_token)
		},
		// 获取用户信息
		async getUserInfoAction() {
			// const { data } = await useUserInfoApi()
			const { data } = userInfoMock
			this.setUser(data)
		},
		// 获取权限
		async getAuthorityListAction() {
			// const { data } = await useAuthorityListApi()
			const { data } = authorityListMock
			this.authorityList = data || []
		},
		// 用户退出
		async logoutAction() {
			// await useLogoutApi()

			// 移除 token
			this.setToken(null)
			this.setRefreshToken(null)
		}
	}
})
